/*
 *  AudioUnitPlayer.cpp
 *  SoundController0002
 *
 *  Created by jiro on 10/07/04.
 *  Copyright 2010 jiro music. All rights reserved.
 *
 */

#include "AudioUnitPlayer.h"
#include <CoreServices/CoreServices.h>

#include <iostream>

namespace sc
{
	AudioUnitPlayer::AudioUnitPlayer( AudioUnitRenderCallback callback )
	{
		ComponentDescription description;
		description.componentType = kAudioUnitType_Output;
		description.componentSubType = kAudioUnitSubType_DefaultOutput;
		description.componentManufacturer = kAudioUnitManufacturer_Apple;
		description.componentFlags = 0;
		description.componentFlagsMask = 0;
		
		Component component = FindNextComponent( NULL, & description );
		OpenAComponent( component, & audioUnit );

		AURenderCallbackStruct callbackStruct;
		callbackStruct.inputProc = callback;
		callbackStruct.inputProcRefCon = this;

		AudioUnitSetProperty(
			audioUnit,
			kAudioUnitProperty_SetRenderCallback,
			kAudioUnitScope_Input,
			0,
			& callbackStruct,
			sizeof( callbackStruct ) );
		AudioUnitInitialize( audioUnit );
	}

	void AudioUnitPlayer::Play( double time )
	{
		AudioOutputUnitStart( audioUnit );
	}
	
	void AudioUnitPlayer::Pause()
	{
		AudioOutputUnitStop( audioUnit );
	}

	void AudioUnitPlayer::Stop()
	{
		AudioOutputUnitStop( audioUnit );
	}

}